package smcms.service.sso.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import smcms.commons.api.entity.result.CommonsResult;
import smcms.commons.api.entity.util.JwtUtil;
import smcms.service.sso.entity.StaffInfoReq;
import smcms.service.sso.entity.StaffInfoResp;
import smcms.service.sso.mapper.StaffMapper;
import smcms.service.sso.service.StaffService;
import smcms.service.sso.utils.UserNameGenerator;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private CommonsResult commonsResult;

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public CommonsResult addCloudStaff(StaffInfoReq req, HttpServletRequest request) {
        //获取请求头中的token
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseToken(token);
        //解析jwt令牌获取cloudId
        req.setCorporationId((String) claims.get("cloudId"));
        if (ObjectUtils.isEmpty(req.getCorporationId())) {
            return commonsResult.failed("公司id不能为空！");
        }
        if (ObjectUtils.isEmpty(req.getRoleName())) {
            return commonsResult.failed("角色名称不能为空");
        }
        //生成UUID
        String uuid = UUID.randomUUID().toString();
        req.setUuid(uuid);
        //初始化账号信息，默认长度11位
        String username = UserNameGenerator.generateUsername(11);
        req.setUsername(username);
        //设置初始密码
        req.setPassword("smcms.123456");
        String corName = staffMapper.queryCloudName(req);
        req.setCloudName(corName);
        staffMapper.addCloudStaff(req);
        //保存到数据库中
        Integer roleId = staffMapper.queryRoleIdByRoleName(req.getRoleName());
        req.setRoleId(roleId.toString());
        staffMapper.addUserRoleRel(req);
        return commonsResult.success("生成员工成功");
    }

    @Override
    public PageInfo<StaffInfoResp> queryCloudStaff(StaffInfoReq req) {
        int pageSize = req.getPageSize() == 0 ? 10 : req.getPageSize();
        int pageNum = req.getPageNum() == 0 ? 1 : req.getPageNum();
        Page<StaffInfoResp> page = PageHelper.startPage(pageNum, pageSize);
        List<StaffInfoResp> staffInfoResps = staffMapper.queryCloudStaff(req);
        PageInfo<StaffInfoResp> pageInfo = page.toPageInfo();
        pageInfo.setList(staffInfoResps);
        return pageInfo;
    }

    @Override
    public CommonsResult deleteStaff(List<String> accountList) {
        if (accountList.isEmpty()) {
            return commonsResult.failed("删除的id不能为空！");
        }
        staffMapper.deleteStaff(accountList);
        return commonsResult.success();
    }

    @Override
    public CommonsResult updateStaff(StaffInfoReq req) {
        if (StringUtils.isBlank(req.getUsername())) {
            return commonsResult.failed("用户名不能为空！");
        }
        staffMapper.updateStaff(req);
        Integer roleId = staffMapper.queryRoleIdByRoleName(req.getRoleName());
        req.setRoleId(roleId.toString());
        staffMapper.updateUserRoleRel(req);
        return commonsResult.success();
    }
}
